# cython: language_level=3
from concurrent.futures import ThreadPoolExecutor, Future
import os
import asyncio
import platform
import time
import app.model_phl.my_tool_package as my_package
import app.utils as utils
import app.utils.util_log as util_log
import datetime
from app.data import RedisUtils
import numpy as np
from utils.redis_utils import create_redis_client, get_redis, getRedisPool, setup_redis
import nest_asyncio


if platform.system() != "Windows":
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
else:
    nest_asyncio.apply()

def get_is_rule_featues(freatures, key):
    if 'none' == freatures.get(key, 'none') or freatures.get(key, 'none') is None:
        util_log.get_logger().warning('get_is_rule_featues {} error'.format(key))
        return False
    else:
        return True

async def async_redis_incrby(key):
        redis_client = await get_redis()
        if key == None:
            redis_key = f"PHL_OLD_CUSTOMER_CNT_{utils.datetime_to_str(datetime.date.today())}"
        else:
            redis_key = key
        apply_cnt = await redis_client.incrby(redis_key, 1)
        # 设置键名为"mykey"的键在当前时间的10秒后过期
        expiration_time = int(time.time()) + 3600 * 24
        await redis_client.expireat(redis_key, expiration_time)
        # Close Redis client
        redis_client.close()
        return apply_cnt

async def future_redis_incrby(future, key):
        redis_client = await get_redis()
        if key == None:
            redis_key = f"PHL_OLD_CUSTOMER_CNT_{utils.datetime_to_str(datetime.date.today())}"
        else:
            redis_key = key
        apply_cnt = await redis_client.incrby(redis_key, 1)
        # 设置键名为"mykey"的键在当前时间的10秒后过期
        expiration_time = int(time.time()) + 3600 * 24
        await redis_client.expireat(redis_key, expiration_time)
        # Close Redis client
        redis_client.close()
        future.set_result(apply_cnt)  # future设置结果
        return apply_cnt

def redis_incrby(future, key=None):
    redis_client = create_redis_client()
    if key == None:
        redis_key = f"PHL_OLD_CUSTOMER_CNT_{utils.datetime_to_str(datetime.date.today())}"
    else:
        redis_key = key
    apply_cnt = redis_client.incrby(redis_key,1)
    # apply_cnt = result.decode('utf-8')
    expiration_time = int(time.time()) + 3600 * 24
    redis_client.expireat(redis_key, expiration_time)
    future.set_result(apply_cnt)
    return apply_cnt

class OldCustomerRules(object):
    """
    老客策略：
    1）拒绝策略
    2）额度策略
    """

    def __int__(self):
        pass

    # 一 拒绝策略模块
    # 1.1  其它规则拒绝策略
    def __ordinary_rule__(self, free_features):
        """
        老客策略
        :param free_features:
        :return:
        """
        rule_sets_result = {}

        # 1 准入规则
        if get_is_rule_featues(free_features, 'on_loan_not_yet_cnt') and int(free_features['on_loan_not_yet_cnt']) > 0:
            rule_sets_result['m102'] = 1

        # 2 多头规则
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 7:
            rule_sets_result['m401'] = 1

        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) >= 2:
            rule_sets_result['m402'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 3:
            rule_sets_result['m403'] = 1

        # 20240823 主产品
        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) < 0 and int(free_features['biz_all_userid_norepay_cnt']) > 1:
            rule_sets_result['m104'] = 1

        # 20240709
        # 老客新增策略
        # 20240809 策略调整
        # if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
        #     if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
        #         rule_sets_result['m105'] = 1
        
        # 20240912 修改m105策略
        # LC和FS一个分支
        # EL和QD一个分支
        if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
            if int(free_features.get('ProjectId', 0)) in ([10086, 10041]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m105'] = 1
            if int(free_features.get('ProjectId', 0)) in ([10077, 10042]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 570:
                    rule_sets_result['m105'] = 1

        # 20240717
        # 增加在贷未还笔数的逻辑
        # biz_all_userid_norepay_cnt > 3
        # 20240912 本平台未还笔数 > 2
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) > 2:
                rule_sets_result['m107'] = 1

        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) <= 3000 and int(free_features['biz_all_userid_norepay_cnt']) >= 2:
            rule_sets_result['m108'] = 1

        # 20240809新增规则
        # 20240912规则阈值修改有1修改为2
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) >= 2:
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m111'] = 1

        # 20240816新增规则
        if int(free_features.get('ProjectId', 0)) in ([10086]):
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) >= 0 and int(free_features['biz_user_last_hkfk_days']) <= 2:
                rule_sets_result['m112'] = 1
        else:
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) == 0:
                rule_sets_result['m112'] = 1

        # 20240809 增加m113规则
        if get_is_rule_featues(free_features, 'biz_userid_pendingloan_cnt') and int(free_features['biz_userid_pendingloan_cnt']) >= 2:
            rule_sets_result['m113'] = 1

        # 20240731 EL老客增加拒贷策略，用于老客全机审
        # 20240812 所有主产品执行
        # if int(free_features.get('ProjectId', 0)) == 10041:
        # 20240912 全平台未还笔数调整阈值，由原先的2调整为3
        # 20240914修改阈值
        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 3:
            rule_sets_result['m109'] = 1
        
        if get_is_rule_featues(free_features, 'biz_curDefault') and int(free_features['biz_curDefault']) > 0:
            rule_sets_result['m410'] = 1

        
        if get_is_rule_featues(free_features, 'biz_maxDpd') and int(free_features['biz_maxDpd']) > 5:
            rule_sets_result['m409'] = 1

        
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 5:
            rule_sets_result['m401'] = 1


        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) > 5:
            rule_sets_result['m402'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 5:
            rule_sets_result['m403'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_history') and int(free_features['biz_max_expire_day_all_real_idcard_history']) > 5:
            rule_sets_result['m404'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_cur') and int(free_features['biz_max_expire_day_all_phone_cur']) > 3:
            rule_sets_result['m420'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_cur') and int(free_features['biz_max_expire_day_all_real_idcard_cur']) > 3:
            rule_sets_result['m421'] = 1


                
        return rule_sets_result

    # 1.2 信用评分拒绝策略
    def __score_rule__(self, free_features):
        """
        信用分规则
        :param free_features:
        :return:
        """
        rule_sets_result = {}

        # 老客信用评分规则
        # ProjectID的编号与产品对应的关系
        # 1）10041 Easy Loan
        # 2）10070 Future cash
        # 3) 10042 LuckyCash

        # Easy Loan产品 复贷的信用评分规则
        # 20240709 增加新的的线下包
        # 20240719 主产品，老客模型评分拒绝都是520
        # 20240729 主产品，老客模型评分拒绝收紧到530
        
        # EasyLoan产品 主模型阿伟老客评分V1
        # LuckyCash产品 主模型自研老客评分V1，放松阈值
        # 20240823 LuckyCash和FastService 阿伟老客评分V1
        if int(free_features.get('ProjectId', 0)) in ([10041]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 520:
                rule_sets_result['m501'] = 1
        elif int(free_features.get('ProjectId', 0)) in ([10042]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 500:
                rule_sets_result['m501'] = 1
        else:
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1



        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 0:
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 540:
                rule_sets_result['m502'] = 1


        if get_is_rule_featues(free_features, 'CreditScoreV4') and int(free_features['CreditScoreV4']) > 0 and int(free_features['CreditScoreV4']) <= 500:
            rule_sets_result['m503'] = 1

        # EasyLoan产品
        # 20240823增加LuckyCash老客
        if int(free_features.get('ProjectId', 0)) in ([10041, 10042]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                rule_sets_result['m506'] = 1

        # 20240823 FS m508 拒贷尾量
        if int(free_features.get('ProjectId', 0)) in ([10077]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 500:
                rule_sets_result['m508'] = 1

        # # Lucky Cash产品 复贷的信用评分规则
        # if int(free_features.get('ProjectId', 0)) in ([10042]):
        #     if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 490:
        #         rule_sets_result['m505'] = 1
        
        # # Future Cash产品 复贷的信用评分规则
        # if int(free_features.get('ProjectId', 0)) == 10070:
        #     if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 520:
        #         rule_sets_result['m501'] = 1

        return rule_sets_result

    def rules(self, free_features):
        """
        老客规则
        :param free_features:
        :return:
        """
        rule_sets_result = {}
        rule_result = self.__ordinary_rule__(free_features=free_features)
        rule_sets_result.update(rule_result)
        score_result = self.__score_rule__(free_features=free_features)
        rule_sets_result.update(score_result)
        return rule_sets_result

    def gentou_rules(self, free_features):
        """
        老客策略
        :param free_features:
        :return:
        """
        rule_sets_result = {}

        # 1 准入规则
        if get_is_rule_featues(free_features, 'on_loan_not_yet_cnt') and int(free_features['on_loan_not_yet_cnt']) > 0:
            rule_sets_result['m102'] = 1

        # 2 多头规则
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 7:
            rule_sets_result['m401'] = 1

        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) >= 2:
            rule_sets_result['m402'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 0:
            rule_sets_result['m403'] = 1

        # 20240607增加基于收益的拒绝策略biz_all_userid_shouyi_amt
        # 20240704增加biz_all_userid_norepay_cnt的限制 biz_all_userid_norepay_cnt > 0
        # 20240823策略重启
        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) < 0 and int(free_features['biz_all_userid_norepay_cnt']) > 0:
            rule_sets_result['m104'] = 1

        # 20240709 
        # 老客新增策略
        # if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
        #     rule_sets_result['m105'] = 1

        # 20240809 策略调整
        # if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
        #     if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
        #         rule_sets_result['m105'] = 1

        # 20240914 修改m105阈值
        if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
            if int(free_features.get('ProjectId', 0)) in ([10090, 10087, 10088, 10089, 10058, 10050, 10052, 10053]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m105'] = 1
            if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081, 10044, 10045, 10046, 10054]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 570:
                    rule_sets_result['m105'] = 1
        
        # 20240717
        # 增加在贷未还笔数的逻辑
        # biz_all_userid_norepay_cnt > 3
        # 20240914修改阈值
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) > 2:
                rule_sets_result['m107'] = 1


        # 20240719增加拒贷策略
        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) <= 3000 and int(free_features['biz_all_userid_norepay_cnt']) >= 2:
            rule_sets_result['m108'] = 1


        # 20240809新增规则
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) >= 1:
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m111'] = 1
                    
        # 20240809QD跟投的拒绝策略
        if int(free_features.get('ProjectId', 0)) in ([10087, 10088, 10089, 10090]):
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) >= 0 and int(free_features['biz_user_last_hkfk_days']) <= 2:
                rule_sets_result['m112'] = 1
        else:
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) == 0:
                rule_sets_result['m112'] = 1

        # 20240809 增加m113规则
        if get_is_rule_featues(free_features, 'biz_userid_pendingloan_cnt') and int(free_features['biz_userid_pendingloan_cnt']) >= 2:
            rule_sets_result['m113'] = 1

        # 20240731 EL老客跟投产品拒绝策略，用于老客跟投全机审
        # 20240812 所有跟放执行
        # if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053]):

        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 3:
            rule_sets_result['m109'] = 1
        
        if get_is_rule_featues(free_features, 'biz_curDefault') and int(free_features['biz_curDefault']) > 0:
            rule_sets_result['m410'] = 1

        
        if get_is_rule_featues(free_features, 'biz_maxDpd') and int(free_features['biz_maxDpd']) > 3:
            rule_sets_result['m409'] = 1

        
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 3:
            rule_sets_result['m401'] = 1


        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) > 3:
            rule_sets_result['m402'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 3:
            rule_sets_result['m403'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_history') and int(free_features['biz_max_expire_day_all_real_idcard_history']) > 3:
            rule_sets_result['m404'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_cur') and int(free_features['biz_max_expire_day_all_phone_cur']) > 0:
            rule_sets_result['m420'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_cur') and int(free_features['biz_max_expire_day_all_real_idcard_cur']) > 0:
            rule_sets_result['m421'] = 1
        
        
        # Easy Loan产品 跟投产品评分规则 We Peso 10058
        # 20240528增加其他产品
        # 'We Peso',  10058 
        # 'Peso Market', 10050
        # 'Smart Money',  10052
        # 'Fortune Cash',  10053
        # 20240607 新增
        # 10078	Good Deal
        # 10079	Save Value
        # 10080	Speed Cash
        # 10081	Cash King
        if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1
        

        # FastService的跟放产品的拒绝策略
        # 10078	Good Deal
        # 10079	Save Value
        # 10080	Speed Cash
        # 10081	Cash King

        # 20240709
        # 增加新的线下包的4个跟放产品的逻辑评分拒绝逻辑
        if int(free_features.get('ProjectId', 0)) in ([10087, 10088, 10089, 10090]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # 20240710修改FastService的跟投评分拒绝阈值
        if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # 20240823 FS m508 拒贷尾量
        if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 510:
                rule_sets_result['m508'] = 1

        # Future Cash产品 跟投产品评分规则 Happy Cash 10071
        # 20240528增加其他产品
        # 'Happy Cash', 10071
        # 'Perfect Cash',  10073
        # 'Urgent loan',  10074
        # 'Urgent cash',  10075
        if int(free_features.get('ProjectId', 0)) in ([10071, 10073, 10074, 10075]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # Lucky Cash产品 跟投产品评分规则 ExpressPera  10044
        # 20240528增加其他产品
        # 'ExpressPera',  10044
        # 'KwartaLoan',   10045 
        # 'CashPao', 10046
        # 'MasayaPera' 10054
        # 20240823 LC和FS的跟放产品使用阿伟评分V1
        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 510:
                rule_sets_result['m501'] = 1

        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m506'] = 1
        
        

        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 0:
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 540:
                rule_sets_result['m502'] = 1


        if get_is_rule_featues(free_features, 'CreditScoreV4') and int(free_features['CreditScoreV4']) > 0 and int(free_features['CreditScoreV4']) <= 500:
            rule_sets_result['m503'] = 1

            
        return rule_sets_result

    def gentou_rules_old(self, free_features):
        """
        老客策略
        :param free_features:
        :return:
        """
        rule_sets_result = {}

        # 1 准入规则
        if get_is_rule_featues(free_features, 'on_loan_not_yet_cnt') and int(free_features['on_loan_not_yet_cnt']) > 0:
            rule_sets_result['m102'] = 1

        # 2 多头规则
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 7:
            rule_sets_result['m401'] = 1

        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) >= 2:
            rule_sets_result['m402'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 0:
            rule_sets_result['m403'] = 1

        # 20240607增加基于收益的拒绝策略biz_all_userid_shouyi_amt
        # 20240704增加biz_all_userid_norepay_cnt的限制 biz_all_userid_norepay_cnt > 0
        # 20240823策略重启
        # 20240914跟放复贷修改阈值
        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) < 0 and int(free_features['biz_all_userid_norepay_cnt']) > 1:
            rule_sets_result['m104'] = 1

        # 20240709 
        # 老客新增策略
        # if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
        #     rule_sets_result['m105'] = 1

        # 20240809 策略调整
        # if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
        #     if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
        #         rule_sets_result['m105'] = 1

        # 20240914 修改m105阈值
        if get_is_rule_featues(free_features, 'biz_all_phone_sour_cnt_7d') and int(free_features['biz_all_phone_sour_cnt_7d']) >= 3:
            if int(free_features.get('ProjectId', 0)) in ([10090, 10087, 10088, 10089, 10058, 10050, 10052, 10053]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m105'] = 1
            if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081, 10044, 10045, 10046, 10054]):
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 570:
                    rule_sets_result['m105'] = 1
        
        # 20240717
        # 增加在贷未还笔数的逻辑
        # biz_all_userid_norepay_cnt > 3
        # 20240914修改阈值
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) > 2:
                rule_sets_result['m107'] = 1


        # 20240719增加拒贷策略
        if get_is_rule_featues(free_features, 'biz_all_userid_shouyi_amt') and get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_shouyi_amt']) <= 3000 and int(free_features['biz_all_userid_norepay_cnt']) >= 2:
            rule_sets_result['m108'] = 1


        # 20240809新增规则
        # 20240914跟放复贷修改阈值
        if get_is_rule_featues(free_features, 'biz_all_userid_norepay_cnt') and int(free_features['biz_all_userid_norepay_cnt']) >= 2:
                if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 550:
                    rule_sets_result['m111'] = 1
                    
        # 20240809QD跟投的拒绝策略
        if int(free_features.get('ProjectId', 0)) in ([10087, 10088, 10089, 10090]):
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) >= 0 and int(free_features['biz_user_last_hkfk_days']) <= 2:
                rule_sets_result['m112'] = 1
        else:
            if get_is_rule_featues(free_features, 'biz_user_last_hkfk_days') and int(free_features['biz_user_last_hkfk_days']) == 0:
                rule_sets_result['m112'] = 1

        # 20240809 增加m113规则
        if get_is_rule_featues(free_features, 'biz_userid_pendingloan_cnt') and int(free_features['biz_userid_pendingloan_cnt']) >= 2:
            rule_sets_result['m113'] = 1

        # 20240731 EL老客跟投产品拒绝策略，用于老客跟投全机审
        # 20240812 所有跟放执行
        # if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053]):
        # 20240914修改阈值
        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 3:
            rule_sets_result['m109'] = 1
        
        if get_is_rule_featues(free_features, 'biz_curDefault') and int(free_features['biz_curDefault']) > 0:
            rule_sets_result['m410'] = 1

        
        if get_is_rule_featues(free_features, 'biz_maxDpd') and int(free_features['biz_maxDpd']) > 3:
            rule_sets_result['m409'] = 1

        
        if get_is_rule_featues(free_features, 'max_overdue_days') and int(free_features['max_overdue_days']) > 3:
            rule_sets_result['m401'] = 1


        if get_is_rule_featues(free_features, 'biz_lastOrderDpd') and int(free_features['biz_lastOrderDpd']) > 3:
            rule_sets_result['m402'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_history') and int(free_features['biz_max_expire_day_all_phone_history']) > 3:
            rule_sets_result['m403'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_history') and int(free_features['biz_max_expire_day_all_real_idcard_history']) > 3:
            rule_sets_result['m404'] = 1


        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_phone_cur') and int(free_features['biz_max_expire_day_all_phone_cur']) > 0:
            rule_sets_result['m420'] = 1

        if get_is_rule_featues(free_features, 'biz_max_expire_day_all_real_idcard_cur') and int(free_features['biz_max_expire_day_all_real_idcard_cur']) > 0:
            rule_sets_result['m421'] = 1
        
        
        # Easy Loan产品 跟投产品评分规则 We Peso 10058
        # 20240528增加其他产品
        # 'We Peso',  10058 
        # 'Peso Market', 10050
        # 'Smart Money',  10052
        # 'Fortune Cash',  10053
        # 20240607 新增
        # 10078	Good Deal
        # 10079	Save Value
        # 10080	Speed Cash
        # 10081	Cash King
        if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1
        

        # FastService的跟放产品的拒绝策略
        # 10078	Good Deal
        # 10079	Save Value
        # 10080	Speed Cash
        # 10081	Cash King

        # 20240709
        # 增加新的线下包的4个跟放产品的逻辑评分拒绝逻辑
        if int(free_features.get('ProjectId', 0)) in ([10087, 10088, 10089, 10090]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # 20240710修改FastService的跟投评分拒绝阈值
        if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # 20240823 FS m508 拒贷尾量
        if int(free_features.get('ProjectId', 0)) in ([10078, 10079, 10080, 10081]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 510:
                rule_sets_result['m508'] = 1

        # Future Cash产品 跟投产品评分规则 Happy Cash 10071
        # 20240528增加其他产品
        # 'Happy Cash', 10071
        # 'Perfect Cash',  10073
        # 'Urgent loan',  10074
        # 'Urgent cash',  10075
        if int(free_features.get('ProjectId', 0)) in ([10071, 10073, 10074, 10075]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m501'] = 1

        # Lucky Cash产品 跟投产品评分规则 ExpressPera  10044
        # 20240528增加其他产品
        # 'ExpressPera',  10044
        # 'KwartaLoan',   10045 
        # 'CashPao', 10046
        # 'MasayaPera' 10054
        # 20240823 LC和FS的跟放产品使用阿伟评分V1
        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if get_is_rule_featues(free_features, 'XWOldScoreV1') and float(free_features['XWOldScoreV1']) <= 510:
                rule_sets_result['m501'] = 1

        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 530:
                    rule_sets_result['m506'] = 1
        
        

        if get_is_rule_featues(free_features, 'biz_all_phone_norepay_cnt') and int(free_features['biz_all_phone_norepay_cnt']) > 0:
            if get_is_rule_featues(free_features, 'oldCreditScoreV1') and int(free_features['oldCreditScoreV1']) <= 540:
                rule_sets_result['m502'] = 1


        if get_is_rule_featues(free_features, 'CreditScoreV4') and int(free_features['CreditScoreV4']) > 0 and int(free_features['CreditScoreV4']) <= 500:
            rule_sets_result['m503'] = 1

            
        return rule_sets_result


    def initial_abtest(self, free_features):
        """
        abtest分流
        :param free_features:
        :return:
        """
            
        apply_cnt = '0'
        redis_key = f"PHL_OLD_CUSTOMER_CNT_{utils.datetime_to_str(datetime.date.today())}"
        
        # apply_cnt = asyncio.run(redis_incrby(redis_key))
        
        executor = ThreadPoolExecutor(max_workers=1)
        future = Future()
        # 提交异步方法执行
        task = executor.submit(redis_incrby, future, redis_key)
        # 阻塞等待异步方法执行完成
        task.result()
        # 获取异步方法的返回值
        apply_cnt = future.result()
        # print(apply_cnt)

        apply_cnt_value = 0 if apply_cnt is None else apply_cnt
        old_high_quota = utils.split_by_cnt(apply_cnt_value, [2, 4, 6, 8, 9])
        # 实验组1|对照组0
        free_features['old_high_quota_v1'] = old_high_quota
        return free_features

    # 二、额度策略模块
    # 20240719老客评分分支模块
    def __get_old_branch_id__(self, free_features):
        oldCreditScore = free_features.get('oldCreditScoreV1', 0)
        # LuckyCash产品的评分分支
        if oldCreditScore <= 540:
            return 1
        elif oldCreditScore <= 550:
            return 2
        elif oldCreditScore <= 560:
            return 3
        elif oldCreditScore <= 580:
            return 4
        else:
            return 5

    # def __get_old_branch_id_v1__(self, free_features):
    #     oldCreditScoreV1 = free_features.get('oldCreditScoreV1', 0)
    #     # EasyLoan产品的评分分支
    #     if oldCreditScoreV1 <= 557:
    #         return 1
    #     elif oldCreditScoreV1 <= 570:
    #         return 2
    #     else:
    #         return 3
        
    # def __get_old_branch_id_el_v1__(self, free_features):
    #     oldCreditScoreV1 = free_features.get('oldCreditScoreV1', 0)
    #     # EasyLoan产品的评分分支
    #     if oldCreditScoreV1 <= 550:
    #         return 1
    #     elif oldCreditScoreV1 <= 565:
    #         return 2
    #     else:
    #         return 3
        
    # def __get_old_branch_id_v2__(self, free_features):
    #     oldCreditScoreV1 = free_features.get('oldCreditScoreV1', 0)
    #     # FutureCash产品的评分分支
    #     if oldCreditScoreV1 <= 553:
    #         return 1
    #     elif oldCreditScoreV1 <= 569:
    #         return 2
    #     else:
    #         return 3
        

    def __preloan_amount__(self, free_features, FraudAssessmentResults):
        """
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        # 20240719LC和EL老客提额更新
        amount = 0
        upAmount = 0
        maxAmount = 20000  # 设置上限额度

        if FraudAssessmentResults == 'reject':
            return amount, upAmount

        current_amount = free_features.get('current_amount', 0)
        # biz_all_phone_repay_count_history = free_features.get('biz_all_phone_repay_count_history', -1)

        biz_all_phone_norepay_cnt = free_features.get('biz_all_phone_norepay_cnt', -1)

        biz_all_userid_norepay_cnt = free_features.get('biz_all_userid_norepay_cnt', -1)
        biz_all_userid_shouyi_amt = free_features.get('biz_all_userid_shouyi_amt', 0)


        # 20240731增加老客不提额策略
        biz_lastOrderDpd = free_features.get('biz_lastOrderDpd', 0)
        biz_max_expire_day_all_phone_cur = free_features.get('biz_max_expire_day_all_phone_cur', 0)
        biz_max_expire_day_all_real_idcard_cur = free_features.get('biz_max_expire_day_all_real_idcard_cur', 0)
        biz_user_last_hkfk_days = free_features.get('biz_user_last_hkfk_days', 0)
        biz_user_loancnt_3d = free_features.get('biz_user_loancnt_3d', 0)

        # 20240809增加老客不提额策略
        biz_all_phone_shouyi_amt = free_features.get('biz_all_phone_shouyi_amt', 0)
        biz_all_phone_sour_cnt_7d = free_features.get('biz_all_phone_sour_cnt_7d', 0)

        amount = current_amount
        branch_id = self.__get_old_branch_id__(free_features)

        XWOldScoreV1 = float(free_features.get('XWOldScoreV1', 0))
        if XWOldScoreV1 is None:
            XWOldScoreV1 = 0

        if int(free_features.get('ProjectId', 0)) == 10042:
            if biz_all_phone_norepay_cnt == 0:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 700
                elif XWOldScoreV1 <= 580:
                    upAmount = 1800
                elif XWOldScoreV1 <= 600:
                    upAmount = 2500
                else:
                    upAmount = 3000
            elif biz_all_phone_norepay_cnt == 1:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 500
                elif XWOldScoreV1 <= 580:
                    upAmount = 1000
                elif XWOldScoreV1 <= 600:
                    upAmount = 1500
                else:
                    upAmount = 2000
            elif biz_all_phone_norepay_cnt == 2:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 0
                elif XWOldScoreV1 <= 580:
                    upAmount = 500
                elif XWOldScoreV1 <= 600:
                    upAmount = 1000
                else:
                    upAmount = 1500
            elif biz_all_phone_norepay_cnt >= 3:
                    upAmount = 0
            else:
                upAmount = 0
        elif int(free_features.get('ProjectId', 0)) == 10041:
            if biz_all_phone_norepay_cnt == 0:
                if XWOldScoreV1 <= 550:
                    upAmount = 500
                elif XWOldScoreV1 <= 560:
                    upAmount = 1000
                elif XWOldScoreV1 <= 580:
                    upAmount = 1500
                elif XWOldScoreV1 <= 600:
                    upAmount = 2500
                else:
                    upAmount = 3000
            elif biz_all_phone_norepay_cnt == 1:
                if XWOldScoreV1 <= 550:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 500
                elif XWOldScoreV1 <= 580:
                    upAmount = 1000
                elif XWOldScoreV1 <= 600:
                    upAmount = 1500
                else:
                    upAmount = 2000
            elif biz_all_phone_norepay_cnt == 2:
                if XWOldScoreV1 <= 550:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 0
                elif XWOldScoreV1 <= 580:
                    upAmount = 0
                elif XWOldScoreV1 <= 600:
                    upAmount = 500
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt >= 3:
                upAmount = 0



        if biz_all_userid_norepay_cnt == 1 and biz_all_userid_shouyi_amt <= 1000:
            upAmount = 0
        if biz_all_userid_norepay_cnt >= 2 and biz_all_userid_shouyi_amt <= 8000:
            upAmount = 0


        # 20240731老客不提额策略
        if biz_lastOrderDpd is None:
            pass
        elif biz_lastOrderDpd > 0:
            upAmount = 0
        
        if biz_max_expire_day_all_phone_cur is None:
            pass 
        elif biz_max_expire_day_all_phone_cur > 0:
            upAmount = 0

        if biz_max_expire_day_all_real_idcard_cur is None:
            pass 
        elif biz_max_expire_day_all_real_idcard_cur > 0:
            upAmount = 0

        if biz_user_last_hkfk_days is None:
            pass 
        elif biz_user_last_hkfk_days < 3:
            upAmount = 0

        if biz_user_loancnt_3d is None:
            pass
        elif biz_user_loancnt_3d > 1:
            upAmount = 0

        # 20240812下线
        # if biz_all_phone_norepay_cnt is None:
        #     pass 
        # elif biz_all_phone_norepay_cnt > 0:
        #     upAmount = 0


        # 20240809 增加2条不提额策略
        if biz_all_phone_shouyi_amt is None:
            pass
        elif biz_all_phone_shouyi_amt < 0:
            upAmount = 0

        if biz_all_phone_sour_cnt_7d is None:
            pass 
        elif biz_all_phone_sour_cnt_7d >= 3:
            upAmount = 0

        amount = amount + upAmount
        if amount > maxAmount:
            amount = maxAmount
        return amount, upAmount


    def __preloan_amount_v1__(self, free_features, FraudAssessmentResults):
        """
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        # 20240719FS、FC和QD提额策略
        amount = 0
        upAmount = 0
        maxAmount = 20000  # 设置上限额度

        if FraudAssessmentResults == 'reject':
            return amount, upAmount

        current_amount = free_features.get('current_amount', 0)

        biz_all_phone_norepay_cnt = free_features.get('biz_all_phone_norepay_cnt', -1)

        biz_all_userid_norepay_cnt = free_features.get('biz_all_userid_norepay_cnt', -1)
        biz_all_userid_shouyi_amt = free_features.get('biz_all_userid_shouyi_amt', 0)

        # 20240731增加老客不提额策略
        biz_lastOrderDpd = free_features.get('biz_lastOrderDpd', 0)
        biz_max_expire_day_all_phone_cur = free_features.get('biz_max_expire_day_all_phone_cur', 0)
        biz_max_expire_day_all_real_idcard_cur = free_features.get('biz_max_expire_day_all_real_idcard_cur', 0)
        biz_user_last_hkfk_days = free_features.get('biz_user_last_hkfk_days', 0)
        biz_user_loancnt_3d = free_features.get('biz_user_loancnt_3d', 0)

        # 20240809增加老客不提额策略
        biz_all_phone_shouyi_amt = free_features.get('biz_all_phone_shouyi_amt', 0)
        biz_all_phone_sour_cnt_7d = free_features.get('biz_all_phone_sour_cnt_7d', 0)

        amount = current_amount
        branch_id = self.__get_old_branch_id__(free_features)

        if biz_all_phone_norepay_cnt == 0:
            if branch_id == 1:
                upAmount = 500
            elif branch_id == 2:
                upAmount = 700
            elif branch_id == 3:
                upAmount = 1000
            elif branch_id == 4:
                upAmount = 1500
            else:
                upAmount = 2000
        elif biz_all_phone_norepay_cnt == 1:
            if branch_id == 1:
                upAmount = 0
            elif branch_id == 2:
                upAmount = 700
            elif branch_id == 3:
                upAmount = 700
            elif branch_id == 4:
                upAmount = 1000
            else:
                upAmount = 1000
        elif biz_all_phone_norepay_cnt == 2:
            if branch_id == 1:
                upAmount = 0
            elif branch_id == 2:
                upAmount = 700
            elif branch_id == 3:
                upAmount = 700
            elif branch_id == 4:
                upAmount = 700
            else:
                upAmount = 1000
        elif biz_all_phone_norepay_cnt >= 3:
                upAmount = 0
        else:
            upAmount = 0

        if biz_all_userid_norepay_cnt == 1 and biz_all_userid_shouyi_amt <= 1000:
            upAmount = 0
        if biz_all_userid_norepay_cnt >= 2 and biz_all_userid_shouyi_amt <= 8000:
            upAmount = 0

        # 20240731老客不提额策略
        if biz_lastOrderDpd is None:
            pass
        elif biz_lastOrderDpd > 0:
            upAmount = 0
        
        if biz_max_expire_day_all_phone_cur is None:
            pass 
        elif biz_max_expire_day_all_phone_cur > 0:
            upAmount = 0

        if biz_max_expire_day_all_real_idcard_cur is None:
            pass 
        elif biz_max_expire_day_all_real_idcard_cur > 0:
            upAmount = 0

        if biz_user_last_hkfk_days is None:
            pass 
        elif biz_user_last_hkfk_days < 3:
            upAmount = 0

        if biz_user_loancnt_3d is None:
            pass
        elif biz_user_loancnt_3d > 1:
            upAmount = 0

        # 20240812 下线
        # if biz_all_phone_norepay_cnt is None:
        #     pass 
        # elif biz_all_phone_norepay_cnt > 0:
        #     upAmount = 0

        # 20240809 增加2条不提额策略
        if biz_all_phone_shouyi_amt is None:
            pass
        elif biz_all_phone_shouyi_amt < 0:
            upAmount = 0

        if biz_all_phone_sour_cnt_7d is None:
            pass 
        elif biz_all_phone_sour_cnt_7d >= 3:
            upAmount = 0

        amount = amount + upAmount
        if amount > maxAmount:
            amount = maxAmount
        return amount, upAmount

    
    # def __abtest_preloan_amount__(self, free_features, FraudAssessmentResults):
    #     """
    #     额度策略
    #     :param free_features:
    #     :param FraudAssessmentResults:
    #     :return:
    #     """
    #     amount = 0
    #     upAmount = 0
    #     maxAmount = 15000

    #     if FraudAssessmentResults == 'reject':
    #         return amount, upAmount


    #     # 获取当前额度，上一笔的额度值
    #     current_amount = free_features.get('current_amount', 0)

    #     # 矩阵组合式提额策略
    #     loanLastOverdueDay = free_features.get('loanLastOverdueDay', 99999)
    #     userPhoneHaveOrder = free_features.get('userPhoneHaveOrder', 0)

    #     amount = current_amount
    #     if loanLastOverdueDay <= 0:
    #         branch_id = self.__get_old_branch_id__(free_features)
    #         if branch_id == 1:
    #             upAmount = 2500
    #         elif branch_id == 2:
    #             upAmount = 2000
    #         elif branch_id == 3:
    #             upAmount = 1200
    #         elif branch_id == 4:
    #             upAmount = 500
    #         elif branch_id == 5:
    #             upAmount = 0
    #         else:
    #             upAmount = 500
    #         if userPhoneHaveOrder > 0:
    #             if branch_id == 2:
    #                 upAmount = upAmount - 800
    #             elif branch_id == 5:
    #                 upAmount = 0
    #             else:
    #                 upAmount = upAmount - 500
    #         upAmount = np.ceil(min(upAmount, current_amount * 0.6) // 100) * 100  # 最大提额幅度60%，向上取整
    #     else:
    #         upAmount = 0

    #     if amount > maxAmount:
    #         amount = maxAmount
    #     return amount, upAmount

    # def inloan_amount(self, free_features, FraudAssessmentResults):
    #     """
    #     老客额度
    #     :param free_features:
    #     :param FraudAssessmentResults:
    #     :return:
    #     """
    #     # 添加标记字段，用于ABTest区分
    #     old_high_quota_v1 = free_features.get('old_high_quota_v1', 0)
    #     if old_high_quota_v1 == 0:
    #         return self.__preloan_amount__(free_features=free_features, FraudAssessmentResults=FraudAssessmentResults)
    #     else:
    #         return self.__abtest_preloan_amount__(free_features=free_features,
    #                                               FraudAssessmentResults=FraudAssessmentResults)
        
    def gentou_pre_amount(self, free_features, FraudAssessmentResults):
        """
        跟投产品，首贷产品做差异化额度管控
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        amount = 0

        if FraudAssessmentResults == 'reject':
            return amount
        
        oldCreditScoreV1 = int(free_features['oldCreditScoreV1'])
        # 跟投产品ExpressPera 做差异化风险定价
        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if oldCreditScoreV1 <= 590:
                amount = 2000
            else:
                amount = 3000
        # 跟投产品We Peso 做差异化风险定价
        # 20240709
        # 新的线下包，跟放产品提额策略
        elif int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053, 10078, 10079, 10080, 10081, 10087, 10088, 10089, 10090]):
            if oldCreditScoreV1 <= 590:
                amount = 2000
            else:
                amount = 3000
        else:
            if oldCreditScoreV1 <= 569:
                amount = 2000
            elif oldCreditScoreV1 <= 581:
                amount = 3000
            else:
                amount = 4000
        return amount 
    

    def gentou_amount_v1(self, free_features, FraudAssessmentResults):
        """
        跟投产品，复贷的提额策略
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        amount = 0
        upAmount = 0
        maxAmount = 12000  # 设置上限额度

        if FraudAssessmentResults == 'reject':
            return amount, upAmount

        current_amount = free_features.get('current_amount', 0)

        biz_all_phone_norepay_cnt = free_features.get('biz_all_phone_norepay_cnt', -1)

        biz_all_userid_norepay_cnt = free_features.get('biz_all_userid_norepay_cnt', -1)
        biz_all_userid_shouyi_amt = free_features.get('biz_all_userid_shouyi_amt', 0)


        # 20240731增加老客不提额策略
        biz_lastOrderDpd = free_features.get('biz_lastOrderDpd', 0)
        biz_max_expire_day_all_phone_cur = free_features.get('biz_max_expire_day_all_phone_cur', 0)
        biz_max_expire_day_all_real_idcard_cur = free_features.get('biz_max_expire_day_all_real_idcard_cur', 0)
        biz_user_last_hkfk_days = free_features.get('biz_user_last_hkfk_days', 0)
        biz_user_loancnt_3d = free_features.get('biz_user_loancnt_3d', 0)

        # 20240809增加老客不提额策略
        biz_all_phone_shouyi_amt = free_features.get('biz_all_phone_shouyi_amt', 0)
        biz_all_phone_sour_cnt_7d = free_features.get('biz_all_phone_sour_cnt_7d', 0)


        amount = current_amount
        # oldCreditScoreV1 = int(free_features['oldCreditScoreV1'])
        
        branch_id = self.__get_old_branch_id__(free_features)

        XWOldScoreV1 = float(free_features.get('XWOldScoreV1', 0))
        if XWOldScoreV1 is None:
            XWOldScoreV1 = 0

        # LC跟放的复贷提额
        if int(free_features.get('ProjectId', 0)) in ([10044, 10045, 10046, 10054]):
            if biz_all_phone_norepay_cnt == 0:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 500
                elif XWOldScoreV1 <= 580:
                    upAmount = 1000
                elif XWOldScoreV1 <= 600:
                    upAmount = 1500
                else:
                    upAmount = 2000
            elif biz_all_phone_norepay_cnt == 1:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 500
                elif XWOldScoreV1 <= 580:
                    upAmount = 700
                elif XWOldScoreV1 <= 600:
                    upAmount = 1000
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt == 2:
                if XWOldScoreV1 <= 530:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 0
                elif XWOldScoreV1 <= 580:
                    upAmount = 700
                elif XWOldScoreV1 <= 600:
                    upAmount = 700
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt >= 3:
                    upAmount = 0
            else:
                upAmount = 0

        # EL跟放提额策略 20240809
        if int(free_features.get('ProjectId', 0)) in ([10058, 10050, 10052, 10053]):
            if biz_all_phone_norepay_cnt == 0:
                if XWOldScoreV1 <= 550:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 700
                elif XWOldScoreV1 <= 580:
                    upAmount = 1000
                elif XWOldScoreV1 <= 600:
                    upAmount = 1500
                else:
                    upAmount = 2000
            elif biz_all_phone_norepay_cnt == 1:
                if XWOldScoreV1 <= 550:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 700
                elif XWOldScoreV1 <= 580:
                    upAmount = 700
                elif XWOldScoreV1 <= 600:
                    upAmount = 1000
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt == 2:
                if XWOldScoreV1 <= 550:
                    upAmount = 0
                elif XWOldScoreV1 <= 560:
                    upAmount = 0
                elif XWOldScoreV1 <= 580:
                    upAmount = 700
                elif XWOldScoreV1 <= 600:
                    upAmount = 700
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt >= 3:
                upAmount = 0
            

        # FC,FS和QD跟放的复贷提额
        if int(free_features.get('ProjectId', 0)) in ([10071, 10073, 10074, 10075, 10078, 10079, 10080, 10081, 10087, 10088, 10089, 10090]):
            if biz_all_phone_norepay_cnt == 0:
                if branch_id == 1:
                    upAmount = 0
                elif branch_id == 2:
                    upAmount = 700
                elif branch_id == 3:
                    upAmount = 1000
                elif branch_id == 4:
                    upAmount = 1500
                else:
                    upAmount = 1500
            elif biz_all_phone_norepay_cnt == 1:
                if branch_id == 1:
                    upAmount = 0
                elif branch_id == 2:
                    upAmount = 700
                elif branch_id == 3:
                    upAmount = 700
                elif branch_id == 4:
                    upAmount = 1000
                else:
                    upAmount = 1000
            elif biz_all_phone_norepay_cnt == 2:
                if branch_id == 1:
                    upAmount = 0
                elif branch_id == 2:
                    upAmount = 0
                elif branch_id == 3:
                    upAmount = 700
                elif branch_id == 4:
                    upAmount = 700
                else:
                    upAmount = 700
            elif biz_all_phone_norepay_cnt >= 3:
                    upAmount = 0
            else:
                upAmount = 0

        if biz_all_userid_norepay_cnt == 1 and biz_all_userid_shouyi_amt <= 1000:
            upAmount = 0
        if biz_all_userid_norepay_cnt >= 2 and biz_all_userid_shouyi_amt <= 8000:
            upAmount = 0


        # 20240731老客不提额策略
        if biz_lastOrderDpd is None:
            pass
        elif biz_lastOrderDpd > 0:
            upAmount = 0
        
        if biz_max_expire_day_all_phone_cur is None:
            pass 
        elif biz_max_expire_day_all_phone_cur > 0:
            upAmount = 0

        if biz_max_expire_day_all_real_idcard_cur is None:
            pass 
        elif biz_max_expire_day_all_real_idcard_cur > 0:
            upAmount = 0

        if biz_user_last_hkfk_days is None:
            pass 
        elif biz_user_last_hkfk_days < 3:
            upAmount = 0

        if biz_user_loancnt_3d is None:
            pass
        elif biz_user_loancnt_3d > 1:
            upAmount = 0

        # 20240812 下线
        # if biz_all_phone_norepay_cnt is None:
        #     pass 
        # elif biz_all_phone_norepay_cnt > 0:
        #     upAmount = 0


        # 20240809 增加2条不提额策略
        if biz_all_phone_shouyi_amt is None:
            pass
        elif biz_all_phone_shouyi_amt < 0:
            upAmount = 0

        if biz_all_phone_sour_cnt_7d is None:
            pass 
        elif biz_all_phone_sour_cnt_7d >= 3:
            upAmount = 0

        amount = amount + upAmount
        if amount > maxAmount:
            amount = maxAmount
        return amount, upAmount


    def inloan_amount_v1(self, free_features, FraudAssessmentResults):
        """
        老客额度管理
        :param free_features:
        :param FraudAssessmentResults:
        :return:
        """
        ProjectId = int(free_features.get('ProjectId', 0))

        # 产品编号和产品名称映射关系
        # 1）10041：Easy Loan
        # 2) 10070: Future cash
        # 3) 10042: LuckyCash
        # 4) 21: Quick Loan
        # 20240709 新的线下包主产品提额策略
        # 5) 10086: Quick Delivery
        if ProjectId in ([10042, 10041]):
            return self.__preloan_amount__(free_features, FraudAssessmentResults)
        else:
            return self.__preloan_amount_v1__(free_features, FraudAssessmentResults)
